import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class JdbcDemo1 {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);


        //1.创建数据源对象
        MysqlDataSource dataSource = new MysqlDataSource();

        //2.配置
        dataSource.setURL("jdbc:mysql://127.0.0.1:3306/java117?characterEncoding=utf8&useSSL=false");
        dataSource.setUser("root");
        dataSource.setPassword("dd.159178280");

        //3.建立网络连接
        Connection connection = dataSource.getConnection();

        //4.构建sql语句
        /*System.out.println("输入名字id");
        int n = scanner.nextInt();
        System.out.println("输入名字");
        String name = scanner.next();*/

        String sql = "select * from student2";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        /*preparedStatement.setInt(1,n);
        preparedStatement.setString(2,name);*/

        //5.执行sql
        /*int i = preparedStatement.executeUpdate();
        System.out.println("affect rows：" + i);*/
        ResultSet resultSet = preparedStatement.executeQuery(sql);
        while (resultSet.next()) {
            int i = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println(i + " " + name);
        }


        //6.关闭资源
        resultSet.close();
        preparedStatement.close();
        connection.close();

    }
}
